import {getStoreList} from "@mall/api/store";
import {storeBindingsBehavior} from "mobx-miniprogram-bindings";
import mall from "@src/store/mall";
import {getDistance} from "@src/common/utils";
import router from "@src/router";
import base from "@src/store/base";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: mall,
    fields: ['curLocation', 'userLocation', 'chooseLocation'],
    actions: ['initLocation', 'setStoreId']
  }, {
    store: base,
    fields: ['themeColor', 'themeTitleColor']
  }],
  properties: {
    deliveryType: {
      type: String
    }
  },
  data: {
    keyword: "",
    originStoreList: [],
    storeList: [],
    disableStoreList: []
  },
  observers: {
    'curLocation': function (curLocation) {
      if (curLocation) {
        this.parseData();
      }
    }
  },
  lifetimes: {
    ready() {
      this.fetchData();
    }
  },
  methods: {
    onPullDownRefresh: function () {
      this.fetchData();
      wx.stopPullDownRefresh();
    },
    fetchData() {
      this.updateStoreBindings()
      getStoreList({}).then(res => {
        let storeList = res.data;
        this.setData({originStoreList: storeList})
        this.parseData(storeList)
      })
    },
    inputBind(e) {
      this.setData({
        keyword: e.detail.value
      })
      this.parseData();
    },
    bindblur(e) {
      this.setData({
        keyword: e.detail.value
      })
      this.parseData();
    },
    parseData() {
      let storeList = [];
      let disableStoreList = [];
      for (let i = 0; i < this.data.originStoreList.length; i++) {
        let store = this.data.originStoreList[i]
        if (this.data.keyword) {
          if (store.name.indexOf(this.data.keyword) === -1) {
            continue
          }
        }

        if (this.data.userLocation) {
          store.userDistance = getDistance(store.latitude, store.longitude,
              this.data.userLocation.latitude, this.data.userLocation.longitude)
        } else {
          store['userDistance'] = -1
        }
        if (this.data.chooseAddress) {
          store['addressDistance'] = getDistance(store.latitude, store.longitude,
              this.data.chooseAddress.latitude, this.data.chooseAddress.longitude)
        } else {
          store['addressDistance'] = -1
        }

        if (this.data.curLocation) {
          store['distance'] = getDistance(store.latitude, store.longitude,
              this.data.curLocation.latitude, this.data.curLocation.longitude)
        } else {
          store['distance'] = -1
        }

        // if (this.data.deliveryType === 'EXPRESS') {
        //   if (store.delivery.supplyExpress) {
            storeList.push(store)
        //   } else {
        //     disableStoreList.push(store)
        //   }
        // } else if (this.data.deliveryType === 'SELF_PICKUP') {
        //   if (store.delivery.supplySelfPickup) {
        //     storeList.push(store);
        //   } else {
        //     disableStoreList.push(store)
        //   }
        // } else if (this.data.deliveryType === 'LOCAL'){
        //   if (store.delivery.supplyLocal) {
        //     storeList.push(store);
        //   } else {
        //     disableStoreList.push(store)
        //   }
        // } else if (this.data.deliveryType === 'LOCAL_SELF_PICKUP') {
        //   if (store.delivery.supplyLocal || store.delivery.supplySelfPickup) {
        //     storeList.push(store);
        //   } else {
        //     disableStoreList.push(store)
        //   }
        // }
      }
      // 排序
      storeList.sort((a, b) => {
        return a.distance - b.distance
      });
      disableStoreList.sort((a, b) => {
        return a.distance - b.distance
      });
      this.setData({storeList: storeList, disableStoreList: disableStoreList})
    },

    handleToStore(e) {
      const store = e.currentTarget.dataset.store
      let  data = {
        store: store,
        deliveryType: this.data.deliveryType
      }
      this.triggerEvent('store', data);
    }
  }
});
